'use strict';

var app = angular.module('proframe', [
    'ngRoute',
    'proframe.view1',
    'proframe.view2',
    'proframe.version',
    'proframe.index',
    'proframe.Auth',
    'proframe.Register',
    'proframe.Create',
    'test'
]).
config(['$locationProvider', '$routeProvider','$httpProvider', function($locationProvider, $routeProvider,$httpProvider) {

  $locationProvider.hashPrefix('!');

    $routeProvider.when('/index', {
        templateUrl: 'views/index.html',
        controller: 'IndexCtrl'
    }).when('/prefect',{
        templateUrl:'views/prefect.html'
    }).when('/ask',{
        templateUrl:'views/ask.html'
    }).when('/answer',{
        templateUrl:'views/answer.html'
    }).when('/create',{
        templateUrl:'views/create.html',
        controller: 'IndexCtrl'
    }).when('/self',{
        templateUrl:'views/self.html'
    })
        .when('/login', {
        templateUrl: 'views/login.html',
        controller: 'LoginController'
    })
        .when('/register', {
            templateUrl: 'views/register.html',
            controller: 'LoginController'
        })
        .otherwise({redirectTo: 'index'});

}]);
app.constant('ConstantBox',{
    serverUrl:"http://localhost:8080",
    user:"游客",
    sessionId:"null"
});
app.factory('ProframeService',['$rootScope','$http', '$q','ConstantBox',function($rootScope,$http,$q,ConstantBox){
    var proservice = {};
   /**
     * 跨域请求
     * @param type 请求类型 GET POST
     * @param url 请求地址
     * @param jsonParam 参数json 类型
     * */
   proservice.jsonpRequest = function(type,url,jsonParam){
       if(type == 'POST' || type == 'post'){
         return proservice.jsonpPost(url,jsonParam);
       }
       if(type == 'GET' || type == 'get'){
           return proservice.jsonpGet(url,jsonParam);
       }

    };
      /**
       * 跨域get请求
       * @param url 请求地址
       * @param jsonParam 参数json 类型
       * */
      proservice.jsonpGet = function(url,jsonParam){
        var deferred = $q.defer();
        var promise = deferred.promise;
        var getUrl =ConstantBox.serverUrl+url;
        $http({
            method: 'GET',
            url: getUrl,
            dataType:'jsonp',
            data:$.param(jsonParam),
            jsonp:"callback"
        }).success(function (data,status,headers,config) {
            console.log(url+' request success',data);
            deferred.resolve(data);
        }).error(function (data,status,headers,config) {
            console.log(url+' request Failed',data);
            deferred.resolve(data);
        });
          return promise;
      };
    /**
     * 跨域post请求
     * @param url 请求地址
     * @param jsonParam 参数json
     * */
    proservice.jsonpPost = function(url,jsonParam){
        var deferred = $q.defer();
        var promise = deferred.promise;
        var postUrl = ConstantBox.serverUrl+url;
        $http({
            method: 'POST',
            url: postUrl,
            dataType:'json',
            headers : { 'Content-Type': 'application/x-www-form-urlencoded' },
            data:$.param(jsonParam),
            jsonp:"callback"
        }).success(function (data,status,headers,config) {
            console.log(url+' request success',data);
            deferred.resolve(data);
        }).error(function (data,status,headers,config) {
            console.log(url+' request Failed',data);
            deferred.resolve(data);
        });
        return promise;
      };
     

    return proservice;
}]);

/**
 * 添加监听
 * */
app.factory('HttpInterceptor',['$q', HttpInterceptor]);

function HttpInterceptor($q){
    return {
        // 请求发出之前，可以用于添加各种身份验证信息
        request: function(config){
            console.log("-------------------------------------------请求之前");
            if(localStorage.token) {
                config.headers.token = localStorage.token;
            }
            return config;
        },
        // 请求发出时出错
        requestError: function(err){
            console.log("-------------------------------------------请求出错");
            return $q.reject(err);
        },
        // 成功返回了响应
        response: function(res){
            console.log("-------------------------------------------请求响应成功");
            return res;
        },
        // 返回的响应出错，包括后端返回响应时，设置了非 200 的 http 状态码
        responseError: function(err){
            console.log("-------------------------------------------请求响应失败");
            return $q.reject(err);
        }
    };
}

/**
 * 添加监听
 * */


